package com.atguigu.mybatisplus.controller;

import com.atguigu.mybatisplus.common.result.Result;
import com.atguigu.mybatisplus.model.form.DeptForm;
import com.atguigu.mybatisplus.model.vo.DeptVO;
import com.atguigu.mybatisplus.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 部门列表
     */
    @GetMapping
    public Result<List<DeptVO>> deptList(String keywords, Long status) {
        List<DeptVO> list = deptService.getDeptList(keywords, status);
        return Result.success(list);
    }

    /**
     * 修改部门
     */
    @PutMapping("/{deptId}")
    public Result<String> updateDept(@PathVariable Long deptId, @RequestBody DeptForm deptForm) {
        deptService.updateDept(deptId, deptForm);
        return Result.success("更新成功");
    }

    /**
     * 新增部门
     */
    @PostMapping
    public Result<String> addDept(@RequestBody DeptForm deptForm) {
        deptService.saveDept(deptForm);
        return Result.success("新增成功");
    }

    /**
     * 删除部门
     */
    @DeleteMapping("/{ids}")
    public Result<String> deleteDept(@PathVariable List<Long> ids) {
        deptService.removeByIds(ids);
        return Result.success("删除成功");
    }

    /**
     * 获取部门表单
     */
    @GetMapping("/{deptId}/form")
    public Result<DeptForm> getDeptForm(@PathVariable Long deptId) {
        DeptForm deptForm = deptService.getFormById(deptId);
        return Result.success(deptForm);
    }
}
